package com.example.mannortycoon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mannortycoon.dao.CompositeDao;
import com.example.mannortycoon.entity.*;
import com.example.mannortycoon.service.*;
import com.example.mannortycoon.uitl.R;
import com.example.mannortycoon.vo.CompositeItemVO;
import com.example.mannortycoon.vo.CompositeVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class CompositeServiceImpl extends ServiceImpl<CompositeDao, CompositeEntity> implements CompositeService {

    private final SeedService seedService;

    private final CropService cropService;

    private final UserCropService userCropService;

    private final UserSeedService userSeedService;

    @Autowired
    public CompositeServiceImpl(SeedService seedService,CropService cropService,
                                UserCropService userCropService,UserSeedService userSeedService){
        this.seedService = seedService;
        this.cropService = cropService;
        this.userCropService = userCropService;
        this.userSeedService = userSeedService;
    }

    @Override
    public Collection<CompositeVO> listDetail() {
        //查询全部
        List<CompositeEntity> list = this.list();
        Map<Long, CompositeVO> map = new HashMap<>();
        list.forEach(item->{
            if(!map.containsKey(item.getSeedId())){
                CompositeVO compositeVO = new CompositeVO();
                compositeVO.setShedId(item.getSeedId());
                //查找种子信息
                SeedEntity seedEntity = seedService.getById(item.getSeedId());
                if(seedEntity != null){
                    compositeVO.setShedImgUrl(seedEntity.getSeedImgUrl());
                    compositeVO.setShedName(seedEntity.getSeedName());
                }
                List<CompositeItemVO> compositeItemVOs = new ArrayList<>();
                CompositeItemVO compositeItemVO = new CompositeItemVO();
                //查询作物信息
                CropEntity cropEntity = cropService.getById(item.getCropId());
                if(cropEntity != null){
                    BeanUtils.copyProperties(cropEntity,compositeItemVO);
                    compositeItemVO.setNumber(item.getNumber());
                }
                compositeItemVOs.add(compositeItemVO);
                compositeVO.setList(compositeItemVOs);
                map.put(item.getSeedId(),compositeVO);
            }else{
                CompositeVO compositeVO = map.get(item.getSeedId());
                CompositeItemVO compositeItemVO = new CompositeItemVO();
                //查询作物信息
                CropEntity cropEntity = cropService.getById(item.getCropId());
                if(cropEntity != null){
                    BeanUtils.copyProperties(cropEntity,compositeItemVO);
                    compositeItemVO.setNumber(item.getNumber());
                }
                compositeVO.getList().add(compositeItemVO);
            }
        });
        return map.values();
    }

    @Override
    public List<CompositeItemVO> listBySeedId(Long seedId) {
        QueryWrapper<CompositeEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("seed_id",seedId);
        List<CompositeEntity> list = this.list(queryWrapper);
        return list.stream().map(item->{
            CompositeItemVO compositeItemVO = new CompositeItemVO();
            compositeItemVO.setCropId(item.getCropId());
            compositeItemVO.setNumber(item.getNumber());
            return compositeItemVO;
        }).collect(Collectors.toList());
    }

    @Override
    @Transactional
    public void saveCompositeBySeeId(Long seedId, List<CompositeEntity> list) {
        //删除该种子所有合成条件
        UpdateWrapper<CompositeEntity> compositeEntityUpdateWrapper = new UpdateWrapper<>();
        compositeEntityUpdateWrapper.eq("seed_id",seedId);
        this.remove(compositeEntityUpdateWrapper);
        //保存合成条件
        this.saveBatch(list);
    }

    @Override
    @Transactional
    public R composite(Long userId, Integer number,Long seedId) {
        //获取该种子的所有合成条件
        QueryWrapper<CompositeEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("seed_id",seedId);
        List<CompositeEntity> list = this.list(queryWrapper);
        if(list.size() == 0){
            return R.error("该种子不存在合成");
        }
        List<UserCropEntity> userCropEntityList = new ArrayList<>();
        for (CompositeEntity item : list) {
            //查询用户该作物
            QueryWrapper<UserCropEntity> userCropEntityQueryWrapper = new QueryWrapper<>();
            userCropEntityQueryWrapper.eq("crop_id", item.getCropId()).gt("number", item.getNumber() * number);
            UserCropEntity one = userCropService.getOne(userCropEntityQueryWrapper);
            if (one == null) {
                return R.error("作物不足");
            }
            one.setNumber(one.getNumber() - item.getNumber() * number);
            userCropEntityList.add(one);
        }
        //添加种子
        //获取用户该种子
        UserSeedEntity userSeedEntity = userSeedService.getById(seedId);
        if(userSeedEntity == null){
            userSeedEntity = new UserSeedEntity();
            userSeedEntity.setSeedId(seedId);
            userSeedEntity.setNumber(number);
            userSeedEntity.setCreateTime(new Date());
            userSeedEntity.setUserId(userId);
            userSeedService.save(userSeedEntity);
        }else{
            userSeedEntity.setNumber(userSeedEntity.getNumber() + 1);
            userSeedService.updateById(userSeedEntity);
        }
        //修改用户作物数量
        userCropService.updateBatchById(userCropEntityList);
        return R.ok();
    }
}
